package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.Staff;
import com.lei.service.StaffService;
import com.lei.service.impl.StaffServiceImpl;
import com.lei.util.Constants;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Lei Chu
 * @Date 2020/10/16 18:08
 */
@WebServlet("/staffServlet/*")
public class StaffServlet extends BaseServlet {

    private final StaffService staffService = new StaffServiceImpl();

    /**
     * 功能描述:检测用户登录
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String checkLogin(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        //把前端传来的表单数据映射成Staff对象
        TransBean.populate(staff, request.getParameterMap());
        Staff resultStaff = staffService.checkLogin(staff);
        if (resultStaff != null) {
            //判断用户的状态，1 为可以登录
            if (resultStaff.getStatus() != 0) {
                //创建Session存储当前登录者的信息
                HttpSession session = request.getSession();
                session.setAttribute("nowStaff", resultStaff);
                // 666 表示登录成功
                return "666";
            } else {
                // 1 为禁止登录  返回的msg的信息为 1 说明此账号被禁止登录
                return "1";
            }
        } else {
            // 返回的msg的信息为 0 说明此账号的账号或者密码错误
            return "0";
        }

    }

    public String exit(HttpServletRequest request,HttpServletResponse response){
        // session销毁
        request.getSession().invalidate();;
        return Constants.REDIRECT + "/index.jsp";
    }

    /**
     * 功能描述:分页查找所有的用户，可带条件查询
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllStaff(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        TransBean.populate(staff, request.getParameterMap());
        PageBean page = staffService.getAllStaff(staff);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:获取最后的 staffId
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getLastStaffId(HttpServletRequest request, HttpServletResponse response) {
        int result = staffService.getLastStaffId();
        return result + "";
    }

    /**
     * 功能描述:添加、修改员工
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String addStaff(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        TransBean.populate(staff, request.getParameterMap());
        int result = staffService.addStaff(staff);
        return result + "";
    }


    /**
     * 功能描述:更改用户密码
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String updatePassword(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        TransBean.populate(staff, request.getParameterMap());
        int result = staffService.updatePassword(staff);
        return result + "";
    }

    /**
     * 功能描述:删除指定用户
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String deleteStaff(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int result = staffService.deleteStaff(Integer.parseInt(id));
        return result + "";
    }

    /**
     * 功能描述:分页查找所有的用户，可带条件查询
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllResignedStaff(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        TransBean.populate(staff, request.getParameterMap());
        PageBean page = staffService.getAllResignedStaff(staff);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:员工离职
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String staffResigned(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int result = staffService.staffResigned(id);
        return result + "";
    }

    /**
     * 功能描述:员工离职后重新入职
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String staffCreated(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        int result = staffService.staffCreated(id);
        return result + "";
    }

    /**
     * 功能描述:查找当前登录的用户的所有信息，用于基本资料的显示
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getNowStaffInformation(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Staff nowStaff = (Staff) session.getAttribute("nowStaff");
        Staff staff = staffService.getNowStaffById(nowStaff);
        return JSON.toJSONString(staff);
    }

    /**
     * 功能描述:员工自己自改自己的手机号和地址
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String updateStaffPhoneAndAdderss(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        TransBean.populate(staff, request.getParameterMap());
        int i = staffService.updateStaffPhoneAndAdderss(staff);
        return i + "";
    }


}
